﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Dictionary.ERP;
using Common;
using Extra.ERP;
using Common.Class;

namespace Dictionary
{
    public class xucProductAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, HRM_PRODUCT item);

        private SimpleButton bbiState;
        private CalcEdit calPrice;
        private ComboBoxEdit cboProductGroup;
        private ComboBoxEdit cboUnit;
        private IContainer components;
        private ImageCollection imageCollection1;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private LabelControl labelControl3;
        private LabelControl labelControl4;
        private LabelControl lcDescription;
        private PictureEdit ptPhoto;
        private TextEdit txtBarcode;
        private MemoEdit txtDescription;

        public xucProductAdd()
        {
            InitializeComponent();
            InitData();
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new HRM_PRODUCT().NewID();
            base.txtNAME.Focus();
        }

        private void AddComboboxEdit(ComboBoxEdit combo, string str)
        {
            combo.Properties.Items.Add(str);
            combo.SelectedIndex = combo.Properties.Items.Count - 1;
        }

        private void bbiState_Click(object sender, EventArgs e)
        {
            new xfmProductState().ShowDialog();
        }

        private void cbo_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            xfmProductGroupAdd.AddedEventHander hander = null;
            xfmUnitAdd.AddedEventHander hander2 = null;
            if (e.Button.Kind == ButtonPredefines.Plus)
            {
                var edit = sender as ComboBoxEdit;
                string name = edit.Name;
                if (name != null)
                {
                    if (!(name == "cboProductGroup"))
                    {
                        if (name == "cboUnit")
                        {
                            var add2 = new xfmUnitAdd(Actions.Add);
                            if (hander2 == null)
                            {
                                hander2 = (s, i) => AddComboboxEdit(cboUnit, i.UnitName);
                            }
                            add2.Added += hander2;
                            add2.ShowDialog();
                        }
                    }
                    else
                    {
                        var add = new xfmProductGroupAdd(Actions.Add);
                        if (hander == null)
                        {
                            hander = (s, i) => AddComboboxEdit(cboProductGroup, i.ProductGroupName);
                        }
                        add.Added += hander;
                        add.ShowDialog();
                    }
                }
            }
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void Init()
        {
        }

        private HRM_PRODUCT InitClass()
        {
            return new HRM_PRODUCT
            {
                ProductCode = base.txtID.Text,
                NameProduct = base.txtNAME.Text,
                ProductGroupCode = cboProductGroup.EditValue.ToString(),
                Barcode = txtBarcode.Text,
                Price = decimal.Parse(calPrice.EditValue.ToString()),
                Unit = cboUnit.Text,
                Description = txtDescription.Text
            };
        }

        private void InitData()
        {
            new HRM_PRODUCT_GROUP().AddComboEdit(cboProductGroup);
            new DIC_UNIT().AddComboEdit(cboUnit);
        }

        private void InitializeComponent()
        {
            components = new Container();
            var manager = new ComponentResourceManager(typeof (xucProductAdd));
            lcDescription = new LabelControl();
            txtDescription = new MemoEdit();
            ptPhoto = new PictureEdit();
            cboProductGroup = new ComboBoxEdit();
            labelControl1 = new LabelControl();
            cboUnit = new ComboBoxEdit();
            txtBarcode = new TextEdit();
            labelControl2 = new LabelControl();
            calPrice = new CalcEdit();
            labelControl3 = new LabelControl();
            labelControl4 = new LabelControl();
            bbiState = new SimpleButton();
            imageCollection1 = new ImageCollection(components);
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            txtDescription.Properties.BeginInit();
            ptPhoto.Properties.BeginInit();
            cboProductGroup.Properties.BeginInit();
            cboUnit.Properties.BeginInit();
            txtBarcode.Properties.BeginInit();
            calPrice.Properties.BeginInit();
            imageCollection1.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(labelControl4);
            base.gcInfo.Controls.Add(labelControl3);
            base.gcInfo.Controls.Add(calPrice);
            base.gcInfo.Controls.Add(labelControl2);
            base.gcInfo.Controls.Add(txtBarcode);
            base.gcInfo.Controls.Add(cboUnit);
            base.gcInfo.Controls.Add(labelControl1);
            base.gcInfo.Controls.Add(cboProductGroup);
            base.gcInfo.Controls.Add(ptPhoto);
            base.gcInfo.Controls.Add(lcDescription);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Size = new Size(0x1ed, 0xf2);
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(lcDescription, 0);
            base.gcInfo.Controls.SetChildIndex(ptPhoto, 0);
            base.gcInfo.Controls.SetChildIndex(cboProductGroup, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl1, 0);
            base.gcInfo.Controls.SetChildIndex(cboUnit, 0);
            base.gcInfo.Controls.SetChildIndex(txtBarcode, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl2, 0);
            base.gcInfo.Controls.SetChildIndex(calPrice, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl3, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl4, 0);
            base.txtNAME.Location = new Point(0x48, 0x34);
            base.txtNAME.Size = new Size(0xf5, 20);
            base.lblName.Location = new Point(7, 0x37);
            base.txtID.Location = new Point(0x48, 0x1a);
            base.txtID.Size = new Size(0xf5, 20);
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            base.btnSave.Location = new Point(0xc4, 0xff);
            base.btnCancel.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            base.btnCancel.Location = new Point(0x183, 0xff);
            base.btnSaveNew.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            base.btnSaveNew.Location = new Point(0x126, 0xff);
            lcDescription.Location = new Point(8, 0xc0);
            lcDescription.Name = "lcDescription";
            lcDescription.Size = new Size(0x27, 13);
            lcDescription.TabIndex = 14;
            lcDescription.Text = "Ghi ch\x00fa:";
            lcDescription.Click += labelControl1_Click;
            txtDescription.Location = new Point(0x48, 0xc0);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0x199, 0x2b);
            txtDescription.TabIndex = 13;
            txtDescription.TabStop = false;
            ptPhoto.Location = new Point(0x143, 0x19);
            ptPhoto.Name = "ptPhoto";
            ptPhoto.Properties.SizeMode = PictureSizeMode.Zoom;
            ptPhoto.Size = new Size(0x9e, 0x9d);
            ptPhoto.TabIndex = 15;
            cboProductGroup.Location = new Point(0x48, 0x4e);
            cboProductGroup.Name = "cboProductGroup";
            cboProductGroup.Properties.Buttons.AddRange(new[]
            {new EditorButton(ButtonPredefines.Combo), new EditorButton(ButtonPredefines.Plus)});
            cboProductGroup.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            cboProductGroup.Size = new Size(0xf5, 20);
            cboProductGroup.TabIndex = 0x10;
            cboProductGroup.ButtonClick += cbo_ButtonClick;
            labelControl1.Location = new Point(8, 0x51);
            labelControl1.Name = "labelControl1";
            labelControl1.Size = new Size(0x3a, 13);
            labelControl1.TabIndex = 0x11;
            labelControl1.Text = "Nh\x00f3m h\x00e0ng:";
            cboUnit.Location = new Point(0x48, 0xa2);
            cboUnit.Name = "cboUnit";
            cboUnit.Properties.Buttons.AddRange(new[]
            {new EditorButton(ButtonPredefines.Combo), new EditorButton(ButtonPredefines.Plus)});
            cboUnit.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            cboUnit.Size = new Size(0xf5, 20);
            cboUnit.TabIndex = 0x12;
            cboUnit.ButtonClick += cbo_ButtonClick;
            txtBarcode.Location = new Point(0x48, 0x68);
            txtBarcode.Name = "txtBarcode";
            txtBarcode.Size = new Size(0xf5, 20);
            txtBarcode.TabIndex = 0x13;
            labelControl2.Location = new Point(8, 0x6b);
            labelControl2.Name = "labelControl2";
            labelControl2.Size = new Size(0x2c, 13);
            labelControl2.TabIndex = 20;
            labelControl2.Text = "M\x00e3 vạch:";
            calPrice.Location = new Point(0x48, 0x85);
            calPrice.Name = "calPrice";
            calPrice.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            calPrice.Properties.Mask.UseMaskAsDisplayFormat = true;
            calPrice.Size = new Size(0xf5, 20);
            calPrice.TabIndex = 0x15;
            labelControl3.Location = new Point(8, 0x88);
            labelControl3.Name = "labelControl3";
            labelControl3.Size = new Size(50, 13);
            labelControl3.TabIndex = 0x16;
            labelControl3.Text = "Tiền c\x00f4ng:";
            labelControl4.Location = new Point(9, 0xa5);
            labelControl4.Name = "labelControl4";
            labelControl4.Size = new Size(0x38, 13);
            labelControl4.TabIndex = 0x17;
            labelControl4.Text = "Đơn vị t\x00ednh:";
            bbiState.ImageIndex = 0;
            bbiState.ImageList = imageCollection1;
            bbiState.Location = new Point(9, 0xff);
            bbiState.Name = "bbiState";
            bbiState.Size = new Size(0x5b, 0x1b);
            bbiState.TabIndex = 0x26;
            bbiState.Text = "C\x00f4ng Đoạn";
            bbiState.Click += bbiState_Click;
            imageCollection1.ImageStream = (ImageCollectionStreamer) manager.GetObject("imageCollection1.ImageStream");
            imageCollection1.Images.SetKeyName(0, "Demo_Struct_Properties.png");
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(bbiState);
            base.Name = "xucProductAdd";
            base.Size = new Size(0x1ee, 0x123);
            base.Load += new EventHandler(xucProductAdd_Load);
            base.Controls.SetChildIndex(bbiState, 0);
            base.Controls.SetChildIndex(base.gcInfo, 0);
            base.Controls.SetChildIndex(base.btnCancel, 0);
            base.Controls.SetChildIndex(base.btnSave, 0);
            base.Controls.SetChildIndex(base.btnSaveNew, 0);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            txtDescription.Properties.EndInit();
            ptPhoto.Properties.EndInit();
            cboProductGroup.Properties.EndInit();
            cboUnit.Properties.EndInit();
            txtBarcode.Properties.EndInit();
            calPrice.Properties.EndInit();
            imageCollection1.EndInit();
            base.ResumeLayout(false);
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {
        }

        private void RaiseSuccessEventHander(HRM_PRODUCT item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(HRM_PRODUCT item)
        {
            base.txtID.Text = item.ProductCode;
            SYS_LOG.Insert("Danh Mục Sản Phẩm", "Xem", base.txtID.Text);
            if (base.m_Status == Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.NameProduct;
            cboProductGroup.EditValue = item.ProductGroupCode;
            txtBarcode.Text = item.Barcode;
            calPrice.EditValue = item.Price;
            cboUnit.Text = item.Unit;
            ptPhoto.Image = item.Photo;
            txtDescription.Text = item.Description;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Actions.Add)
            {
                var hrm_product = new HRM_PRODUCT();
                if (hrm_product.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var hrm_product = new HRM_PRODUCT();
                if ((base.m_Status == Actions.Add) && hrm_product.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new HRM_PRODUCT
            {
                ProductCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            SYS_LOG.Insert("Danh Mục Sản Phẩm", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            HRM_PRODUCT item = InitClass();
            string text = item.Insert();
            if (text == "OK")
            {
                item.Update(base.txtID.Text, ptPhoto.Image);
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            SYS_LOG.Insert("Danh Mục Sản Phẩm", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            HRM_PRODUCT item = InitClass();
            string text = item.Update();
            if (text == "OK")
            {
                text = item.Update(base.txtID.Text, ptPhoto.Image);
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }

        private void xucProductAdd_Load(object sender, EventArgs e)
        {
        }
    }
}